package cn.avalon.org.controller;

import cn.avalon.basic.utils.AjaxResult;
import cn.avalon.basic.utils.PageList;
import cn.avalon.org.domain.Employee;
import cn.avalon.org.query.EmployeeQuery;
import cn.avalon.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 员工
 * @author: Avalon
 * @date: 2022年08月14日 ： 17:47
 * @version: ver1.0
 * @email 709785494@qq.com
 */
@Api(value = "员工接口类",description = "部门接口类")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * @description: 查询所有
     * @author: Avalon
     * @date: 2022/8/14 : 10:12
     * @param:
     * @return:
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    /**
     * @description: 查询单个
     * @author: Avalon
     * @date: 2022/8/14 : 10:12
     * @param:
     * @return:
     */
    @ApiOperation(value = "查询单个")
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id) {
        return employeeService.findById(id);
    }

    /**
     * @description: 添加或修改
     * @author: Avalon
     * @date: 2022/8/14 : 10:26
     * @param:
     * @return:
     */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * @description: 删除接口
     * @author: Avalon
     * @date: 2022/8/14 : 10:26
     * @param:
     * @return:
     */
    @ApiOperation(value = "删除接口")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            employeeService.remove(id);
            return new AjaxResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * @description: 分页接口
     * @author: Avalon
     * @date: 2022/8/14 : 10:31
     * @param:
     * @return:
     */
    @ApiOperation(value = "分页接口")
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }

    @ApiOperation(value = "批量删除接口")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    @GetMapping("/list/{id}")
    public List<Employee> findShopEmployee(@PathVariable("id") Long shopId) {
        return employeeService.findShopEmployee(shopId);
    }
}
